package com.bokesoft.yigo.mid.function;

import com.bokesoft.yes.mid.base.MidVE;
import com.bokesoft.yigo.bpm.StartInstance;
import com.bokesoft.yigo.common.util.TypeConvertor;
import com.bokesoft.yigo.meta.dataobject.MetaDataObject;
import com.bokesoft.yigo.meta.factory.IMetaFactory;
import com.bokesoft.yigo.mid.base.DefaultContext;
import com.bokesoft.yigo.mid.document.SaveData;
import com.bokesoft.yigo.mid.parser.BaseMidFunctionImpl;
import com.bokesoft.yigo.parser.BaseFunImplCluster;
import com.bokesoft.yigo.parser.IExecutor;
import com.bokesoft.yigo.struct.datatable.DataTable;
import com.bokesoft.yigo.struct.document.Document;
import com.bokesoft.yigo.tools.document.DocumentUtil;
/**
 * 函数集合
 * SetFixValue，SetDValue1，DeleteDValue2对应表单Example2保存前处理
 * Example3ToExample4对应表单Example3，保存后处理：每条明细生成一条Example4的数据并保存
 * AutoStartExample5对应表单Example5，保存后处理，自动触发流程
 * @author ljd
 *
 */
public class ExampleFunction extends BaseFunImplCluster {
	//给某个表格的第一行的某个字段赋值
	class SetFixValueImpl extends BaseMidFunctionImpl {

		@Override
		public Object evalImpl(String name, DefaultContext context, Object[] args, IExecutor executor) throws Throwable {
			Document document = context.getDocument();
			String tableName = (String)args[0];
			String columnKey = (String)args[1];
			Object value = args[2];
			DataTable tbl = document.get(tableName);
			if ( tbl.first() ) {
				tbl.setObject(columnKey, value);
			}
			
			return true;
		}
		
	}
	//给某个表格的某列循环赋值
	class SetDValue1Impl extends BaseMidFunctionImpl {
		
		@Override
		public Object evalImpl(String name, DefaultContext context, Object[] args, IExecutor executor) throws Throwable {
			Document document = context.getDocument();
			DataTable tbl = document.get("Example2Detail");
			tbl.beforeFirst();
			while ( tbl.next() ) {
				tbl.setObject("DValue1", "generated string");
			}
			
			return true;
		}
		
	}
	//删除列DValue2=2的行
	class DeleteDValue2Impl extends BaseMidFunctionImpl {

		@Override
		public Object evalImpl(String name, DefaultContext context, Object[] args, IExecutor executor) throws Throwable {
			Document document = context.getDocument();
			DataTable tbl = document.get("Example2Detail");
			
			tbl.first();
			while ( tbl.isValid() ) {
				Integer tmpValue = TypeConvertor.toInteger(tbl.getObject("DValue2"));
				if ( tmpValue.equals(2) ) {
					tbl.delete();
				} else {
					tbl.next();
				}
			}
			
			return true;
		}
		
	}
	//DataTable复制并保存
	class Example3ToExample4Impl extends BaseMidFunctionImpl {
		//Example3Detail(Value1,Value2)-->Example4Head
		@Override
		public Object evalImpl(String name, DefaultContext context, Object[] args, IExecutor executor) throws Throwable {
			//构造Example4
			MidVE ve = context.getVE();
			IMetaFactory metaFactory = ve.getMetaFactory();
			MetaDataObject tgtMetaDataObject = metaFactory.getDataObject("Example4");
			
			Document srcDocument = context.getDocument();
			DataTable srcTbl = srcDocument.get("Example3Detail");
			srcTbl.beforeFirst();
			while ( srcTbl.next() ) {
				String value1 = srcTbl.getString("Value1");
				String value2 = srcTbl.getString("Value2");
				
				// 创建一个空的Document，其中每个头表均有一行空数据
				Document tgtDocument = DocumentUtil.newDocument(tgtMetaDataObject);
				// 设置文档为新增状态，否则后续保存会报文档失效
				tgtDocument.setNew();
				DataTable tgtTbl = tgtDocument.get("Example4Head");
				tgtTbl.setString("Value1", value1);
				tgtTbl.setString("Value2", value2);
				
				// 为每一个需要保存的Document创建一个新的上下文，其与原来的上下文共享同一个数据库连接
				DefaultContext tgtContext = new DefaultContext(context);
				SaveData save = new SaveData(tgtMetaDataObject, null, tgtDocument);
				save.save(tgtContext);
			}
			
			return true;
		}
		
	}
	//根据某个表的某列启动流程
	class AutoStartExample5Impl extends BaseMidFunctionImpl {

		@Override
		public Object evalImpl(String name, DefaultContext context, Object[] args, IExecutor executor) throws Throwable {
			Document document = context.getDocument();
			DataTable tbl = document.get("Example5Head");
			tbl.first();
			boolean autoStart = TypeConvertor.toBoolean(tbl.getObject("AutoStart"));
			if ( autoStart ) {
				long instanceID = TypeConvertor.toLong(tbl.getObject("InstanceID"));
				if ( instanceID <= 0 ) {
					StartInstance start = new StartInstance();
					String formKey = "Example5";
					// 如果需要指定流程来启动，给processKey赋指定的过程标识
					String processKey = null;
					start.start(context, document, formKey, processKey);
				}
			}
			return true;
		}
		
	}

	@Override
	public Object[][] getImplTable() {
		return new Object[][] {
			{ "SetFixValue", new SetFixValueImpl() },
			{ "SetDValue1", new SetDValue1Impl() },
			{ "DeleteDValue2", new DeleteDValue2Impl() },
			{ "Example3ToExample4", new Example3ToExample4Impl() },
			{ "AutoStartExample5", new AutoStartExample5Impl() }
		};
	}

}
